import 'dart:convert';
import 'dart:typed_data';

import 'package:crypto/crypto.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:leader_doctor_flutter/common/data/app.dart';
import 'package:leader_doctor_flutter/common/utils/string_utils.dart';
import 'package:uuid/uuid.dart';

///
/// 通用header参数
///
class CommentHeader {
  static Map<String, String>? header(Map<String, String>? headers) {
    var env = dotenv.env['ENV_NAME'];
    var appKey = '6a9c4160fb813859190f8d307990a4f1';
    var appSecret = "e8dd6aae2043";
    var uuid = const Uuid();
    var nonCe = uuid.v4().toString().replaceAll("-", "");
    var curTime = (DateTime.now().millisecondsSinceEpoch ~/ 1000).toString();
    var content = appSecret + nonCe + curTime;
    var checkSum = StringUtils.dartSha1(content);
    headers = headers ?? {};
    //token
    headers['AppKey'] = appKey;
    headers['Nonce'] = nonCe;
    headers['CurTime'] = curTime;
    headers['CheckSum'] = checkSum;
    headers['local_debug'] = dotenv.env['${env}_SERVICE_IP']!;
    //设备和包相关
    headers["appSystemVersion"] = App.instance.deviceInfo?.systemVersion ?? '';
    headers["appModel"] = App.instance.deviceInfo?.deviceModel ?? '';
    headers["appVersion"] = App.instance.packageInfo?.appVersionName ?? '';
    headers["mobilePhoneNickname"] = App.instance.deviceInfo?.nickname ?? '';
    headers["userId"] = App.instance.userInfo?.id ?? '';
    headers["grayId"] = App.instance.userInfo?.id ?? '';
    headers["remarkz"] = '';
    headers["User-Source"] = App.instance.userInfo?.onLineMode ?? '';
    headers["X-Tenant-Id"] = App.instance.userInfo?.tenantId ?? '';
    return headers;
  }

  ///使用sha1加密
  static String generateSha1(String data) {
    Uint8List content = const Utf8Encoder().convert(data);
    Digest digest = sha1.convert(content);
    return digest.toString();
  }
}
